import Vue from 'vue'
import VueRouter from 'vue-router'

// import Login from '../components/Login.vue'
// import Home from '../components/Home.vue'
// import Welcome from '../components/Welcome.vue'
// 路由懒加载
const Login = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/Login.vue')
const Home = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/Home.vue')
const Welcome = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/Welcome.vue')

// import Users from '../components/user/Users.vue'
// import Rights from '../components/power/Rights.vue'
// import Roles from '../components/power/Roles.vue'
const Users = () => import(/* webpackChunkName:"Users_Rights_Roles" */ '../components/user/Users.vue')
const Rights = () => import(/* webpackChunkName:"Users_Rights_Roles" */ '../components/power/Rights.vue')
const Roles = () => import(/* webpackChunkName:"Users_Rights_Roles" */ '../components/power/Roles.vue')

// import Cate from '../components/goods/Cate.vue'
// import Params from '../components/goods/Params.vue'
const Cate = () => import(/* webpackChunkName:"Cate_Params" */ '../components/goods/Cate.vue')
const Params = () => import(/* webpackChunkName:"Cate_Params" */ '../components/goods/Params.vue')

// import GoodList from '../components/goods/List.vue'
// import Add from '../components/goods/Add.vue'
const GoodList = () => import(/* webpackChunkName:"GoodList_Add" */ '../components/goods/List.vue')
const Add = () => import(/* webpackChunkName:"GoodList_Add" */ '../components/goods/Add.vue')

// import Orders from '../components/order/Orders.vue'
// import Report from '../components/report/Report.vue'
const Orders = () => import(/* webpackChunkName:"Orders_Report" */ '../components/order/Orders.vue')
const Report = () => import(/* webpackChunkName:"Orders_Report" */ '../components/report/Report.vue')

Vue.use(VueRouter)

const router = new VueRouter({
  routes :[
    {     //重定向路由规则   这个path不是我们自己写的，是根据elementUI 的路由写的
      path : '/',
      redirect : '/login'
    },
    {
      path: '/login',
      name: 'Login', 
      component: Login    //路由中component一定不能是字符串
    },
    {
      path: '/home',
      name: 'Home',
      component : Home,
      redirect : '/welcome',
      children:[
        {
          path : '/welcome',
          name : 'Welcome',
          component : Welcome
        },
        {
          path : '/users',
          name : 'Users',
          component : Users
        },
        // 权限管理内容
        {
          path : '/rights',  //权限列表
          name : 'Rights',
          component : Rights
        },
        {
          path : '/roles', // 人员角色
          name : 'Roles',
          component : Roles
        },
        // 商品管理
        {
          path : '/categories', //商品分类
          name : 'Cate',
          component : Cate
        },
        {
          path : '/params', //分类参数
          name : 'Params',
          component : Params
        },
        {
          path : '/goods', // 商品列表
          name : 'List',
          component : GoodList,
        },
        {
          path : '/goods/add', // 添加商品列表
          name : 'Add',
          component : Add,
        },
        // 订单管理
        {
          path: '/orders',
          name: 'Orders',
          component: Orders
        },
        // 数据统计： 数据报表
        {
          path: '/reports',
          name: 'Report',
          component: Report
        }
      ]
    },
  ]
})

// 挂载路由守卫
router.beforeEach((to , from, next ) => {
    // to 将要访问的路径
    // from 代表从哪个路径跳转过来
    // next 是代表放行
    //  next() 放行     next('./login') 强制放行
    if(to.path === '/login') return next()  //如果在登录页面 则放行
    // 判断是否有token  
    const token = window.sessionStorage.getItem('token') //getItem('token') 获得token
    if(!token) return next('./login')  //没有token 则强制转到 .login
    next()

})



export default router
